//
//  HXProductViewController.m
//  爱鲜蜂
//
//  Created by 建国 on 16/4/18.
//  Copyright © 2016年 建国. All rights reserved.
//
#import "HXCategoryViewController.h"
#import "HXProductCell.h"
#import "HXProductViewController.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "HXProducts.h"
#import "HXCategoryViewController.h"
@interface HXProductViewController ()

@property (nonatomic,strong) NSIndexPath *index ;

@property (nonatomic,strong) NSArray *productArray;

@property (nonatomic,strong) NSArray *productCatalog;

@property (nonatomic,strong) NSDictionary *productData;

@end

@implementation HXProductViewController

static NSString *productID = @"HXProductCell";

//所有的商品
-(NSDictionary *)productData{

    if (_productData == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"supermarket" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        [json writeToFile:@"/Users/hongdongheng/Desktop/data.plist" atomically:YES];
        
        _productData = json[@"data"][@"products"];

    }
    
    return _productData;
}

//懒加载商品目录
-(NSArray *)productCatalog{
    if (_productCatalog == nil) {
        
        _productCatalog = @[@"103532",@"103536",@"103541",@"103549",@"103557",@"103565",@"103569",@"103575",@"103581"];
     }


    return _productCatalog;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置ui
    [self setupUI];
    
    //默认选中第一个
    NSDictionary *dict = @{@"row":@0};
    
    [self setupData:dict];
    
    //采用通知的方法
    //接收通知

    [[NSNotificationCenter defaultCenter] addObserverForName:@"CategroySelectedRow" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {

        NSDictionary *dict = note.userInfo;

        [self setupData:dict];
        
    }];
}

//设置UI
-(void)setupUI{

    self.tableView.backgroundColor = [UIColor grayColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXProductCell" bundle:nil] forCellReuseIdentifier:productID];
    self.tableView.rowHeight = 80 ;
    
}


//设置数据
-(void)setupData:(NSDictionary *) dict{
    
    NSInteger catalog = [dict[@"row"] integerValue];

    NSString *str = self.productCatalog[catalog];
    
    NSArray *productsArray = self.productData[str];
    
    NSMutableArray *products = [NSMutableArray array];
    //字典数组转模型
    for (NSDictionary *dict in productsArray) {
        HXProducts *cate = [HXProducts mj_objectWithKeyValues:dict];
        //进行赋值
        [products addObject:cate];
    }
    
    self.productArray = products;
    //刷新数据
    [self.tableView reloadData];

}



//移除通知
-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.productArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HXProductCell *cell = [tableView dequeueReusableCellWithIdentifier:productID forIndexPath:indexPath];
    //传递模型过去
    cell.products = self.productArray[indexPath.row];
    
    return cell ;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%zd",_index.row];
}

@end
